<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('managecomment');


PrintHeader('管理评论');


$action = ForceIncomingString('action', 'displaycomments');
if(IsPost('updatecomments')) $action= 'updatecomments';
if(IsPost('deletecomments')) $action= 'deletecomments';

// ############################## FUNCTIONS #############################

function GetSearchSql($search, $actived) {
	$sWhere = '';
	if($search != ''){
		$sWhere .= " WHERE (username LIKE '%".$search."%' OR content LIKE '%".$search."%') ";

		if($actived == '1'){
			$sWhere .= " AND actived = '1' ";
		}elseif($actived == '-1'){
			$sWhere .= " AND actived = '0' ";
		}
	}elseif($actived == '1'){
		$sWhere .= " WHERE actived = '1' ";
	}elseif($actived == '-1' ){
		$sWhere .= " WHERE actived = '0' ";
	}

	return $sWhere;
}

// ################################ UPDATE COMMENT ################################

if($action == 'updatecomment')
{
	$commentid     = ForceIncomingInt('commentid');
	$username        = ForceIncomingString('username');
	$content        = ForceIncomingString('content');
	$actived     = ForceIncomingInt('actived');
	$deletecomment     = ForceIncomingInt('deletecomment');

	if ($deletecomment)
	{
		$DB->query("DELETE FROM " . TABLE_PREFIX . "comments WHERE commentid = '$commentid' ");
		PrintRedirect('admin.comments.php', 1);
	}

	if (strlen($username) == 0)
	{
		$errors[] = "评论人不能为空!";
	}

	if(strlen($content) == 0){
		$errors[] = "评论内容不能为空!";
	}

	if(!isset($errors)){
		$DB->query("UPDATE " . TABLE_PREFIX . "comments SET 
		username     = '$username',
		content     = '$content',
		actived     = '$actived'
		WHERE commentid   = '$commentid'");

		PrintRedirect('admin.comments.php', 1);
	}else{
		PrintErrors($errors, '编辑评论错误');
		$action = 'editcomment';
	}
}


// ############################### UPDATE COMMENTS ################################

if($action == 'updatecomments'){
	$commentids   = ForceIncomingArray('updatecommentids');
	$activeds   = ForceIncomingArray('activeds');

    for($i = 0; $i < count($commentids); $i++)
    {
		$DB->query("UPDATE " . TABLE_PREFIX . "comments SET	actived = '$activeds[$i]' WHERE commentid = '$commentids[$i]'");
    }

    PrintRedirect('admin.comments.php', 1);

}

// ############################### DELETE COMMENTS ################################

if($action == 'deletecomments')
{
	$deletecommentids = ForceIncomingArray('deletecommentids');

	for($i = 0; $i < count($deletecommentids); $i++){
		$DB->query("DELETE FROM " . TABLE_PREFIX . "comments WHERE commentid = '$deletecommentids[$i]' ");
	}

	PrintRedirect('admin.comments.php', 1);
}

// ############################ DISPLAY COMMENT FORM #############################

if($action == 'editcomment')
{
	$commentid = ForceIncomingInt('commentid');

	if(isset($errors))
	{
		$comment = array('commentid'   => $commentid,
			  'username'     => $username,
			  'content'     => $_POST['content'],
			  'actived'     => $actived);

	}elseif($commentid) {
		$comment = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "comments WHERE commentid = '$commentid'");
	}

	PrintSection('编辑评论');

	echo '<form method="post" action="admin.comments.php">
	<input type="hidden" name="action" value="updatecomment" />
	<input type="hidden" name="commentid" value="' . $comment['commentid'] . '" />
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>评论人:</b></td>
	<td class="tdrow3"><input type="text" name="username" value="'.$comment['username'].'" size="20" /></td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>评论内容:</b></td>
	<td class="tdrow3"><textarea name="content" style="height:160px;width:600px;" id="description">'.$comment['content'].'</textarea></td>
	</tr>	
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>是否发布:</b></td>
	<td class="tdrow3"><input type="checkbox" name="actived" value="1" '.Iif($comment['actived'] == 1, 'CHECKED').'> <b>是:</b> 是否发布此评论?</td>
	</tr>
	<tr>
	<td class="tdrow2" nowrap="nowrap"><b>是否删除:</b></td>
	<td class="tdrow3"><input type="checkbox" name="deletecomment" value="1"> <b>是:</b> <span class=ohred>是否删除此条评论?</span></td>
	</tr>
	<tr>
	<td class="tdrow1" bgcolor="#FCFCFC" colspan="2" align="center">
	<input type="submit" value="保存更新" />
	</td>
	</tr>
	</table>
	</form>';

	EndSection();
}


// ############################### DISPLAY COMMENTS ###############################

if($action == 'displaycomments')
{
	$NumPerPage =20;

	$page = ForceIncomingInt('page', 1);
	$search = ForceIncomingString('s');
	$actived = ForceIncomingInt('a');

	if(IsGet('s')){
		$search = urldecode($search);
	}

	$start = $NumPerPage * ($page-1);

	PrintSection('搜索评论');
	echo '<form method="post" action="admin.comments.php" name="searchcomments">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow2" align="center">搜索:&nbsp;<input type="text" name="s" size="22">&nbsp;&nbsp;选择:&nbsp;<select name="a">
	<option value="0">全部评论</option>
	<option style="color:red;" value="-1">待审评论</option>
	<option value="1">已发布的评论</option>
	</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="searchcomment" value="搜索评论" /></td>
	</tr>
	</table></form>';
	EndSection();

	$title = Iif(IsPost('searchcomment'), '搜索到的评论列表', '全部评论列表');

	$getcomments = $DB->query("SELECT * FROM " . TABLE_PREFIX . "comments ".GetSearchSql($search, $actived)." ORDER BY actived ASC, commentid DESC LIMIT $start,$NumPerPage");

	$maxrows = $DB->query_first("SELECT COUNT(commentid) AS value FROM " . TABLE_PREFIX . "comments ".GetSearchSql($search, $actived));

	PrintSection($title.'('.$maxrows['value'].'个)');

	echo '<form method="post" action="admin.comments.php" name="commentsform">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1" nowrap="nowrap">评论内容</td>
	<td class="tdrow1" nowrap="nowrap">发布人</td>
	<td class="tdrow1" nowrap="nowrap">状态</td>
	<td class="tdrow1" nowrap="nowrap">日期</td>
	<td class="tdrow1" nowrap="nowrap">图片ID</td>
	<td class="tdrow1" nowrap="nowrap"><input type="checkbox" checkall="group" onclick="select_deselectAll (\'commentsform\', this, \'group\');"> 删除</td>
	</tr>';

	if($maxrows['value'] < 1){
		echo '<tr><td class="tdrow3" colspan="10" align="center" height="48" valign="middle"><font class=ohredb>未搜索到任何评论!</font></td></tr>';
	}else{
		while($comment = $DB->getrow($getcomments))
		{
			echo '<tr>
			<td class="tdrow3"><input type="hidden" name="updatecommentids[]" value="'.$comment['commentid'].'" /><a href="admin.comments.php?action=editcomment&commentid=' . $comment['commentid']. '" title="'.$comment['content'].'">'.Iif($comment['actived'] != '1','<font class=ohred><u>').ShortTitle($comment['content'], 48).Iif($comment['actived'] != '1','</u></font>'). '</a></td>
			<td class="tdrow3" nowrap="nowrap">' . $comment['username'] . '</td>
			<td class="tdrow2"><select name="activeds[]">
			<option value="1">发布</option>
			<option style="color:red;" value="0" ' . Iif(!$comment['actived'], 'SELECTED', '') . '>待审</option>
			</select></td>
			<td class="tdrow2" nowrap="nowrap">' . DisplayDate($comment['created'], 'Y-m-d') . '</td>
			<td class="tdrow2"><a href="'.BASEURL.'InterPhoto.php?id='.$comment['imageid'].'" target="_blank">' . $comment['imageid'] . '</a></td>
			<td class="tdrow3"><input type="checkbox" name="deletecommentids[]" value="' . $comment['commentid'] . '" checkme="group" /></td>
			</tr>';
		}
	}

	$totalpages = ceil($maxrows['value'] / $NumPerPage);

	if($totalpages > 1){
		echo '<tr><td class="tdrow3" colspan="6" align="center" height="48" valign="middle">';
		echo GetPageList('admin.comments.php', $totalpages, $page, 10, 's', urlencode($search), 'a', $actived);
		echo '</td></tr>';
	}

	if($maxrows['value'] > 0){
		echo '<tr><td class="tdrow1" colspan="6" align="center"><input type="submit" name="updatecomments" value=" 保存更新 " />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="deletecomments" onclick="return confirm(\'确定删除所选评论吗?\');" value=" 删除评论 " /></td></tr>';
	}

	echo '</table></form>';

	EndSection();
}


// ############################### PRINT FOOTER ################################

PrintFooter();

?>